import { users, menuTreeData } from "../data"

export function getUserInfo() {
    return {
        url: 'api/sysUser/getUserInfo',
        method: "get",
        response: (opts) => {
            const token = localStorage.getItem("Authorization")
            if (token) {
                const uinfo = { ...users.find((v) => v.username === token.split('@')[0])}
                delete uinfo.password
                return {
                    code: 200,
                    msg: "",
                    data: {
                        ...uinfo,
                        'unReadCount|0-10': 0
                    }
                }
            } else {
                return {
                    code: -2,
                    msg: "请先登录！"
                }
            }
        }
    }
}

export function getMenuTree() {
    return {
        url: "api/sysUser/getMenuTree",
        method: "get",
        response: (opts) => {
            const token = localStorage.getItem("Authorization")
            if (!token) {
                return {
                    code: 200,
                    msg: ''
                }
            }
            const username = token.split('@')[0]
            const info = users.find((v) => v.username === username)
            const role = info.roleId
            let treeData = [menuTreeData[2]]
            switch (role) {
                case "admin":
                    treeData = menuTreeData
                    break
                case "master":
                    treeData = [menuTreeData[0], menuTreeData[2]]
                    break
                case "visitor":
                    treeData = [menuTreeData[2]]
                    break
                default:
                    break            
            }

            return {
                code: 200,
                data: treeData
            }
        }
    }
}